package com.actionpals.components.controls
{
	import com.actionpals.enum.InvalidationType;
	import com.actionpals.enum.ScrollPolicy;
	
	import mx.events.ScrollEvent;

	public class TextArea extends BaseTextField
	{
		protected var _textHasChanged:Boolean = false;
		protected var _verticalScrollPolicy:String = ScrollPolicy.AUTO;
		protected var _wordWrap:Boolean = true;
		
		public function TextArea()
		{
			super();
		}
		
		public override function set horizontalScrollPosition( value:int ):void
		{
			drawNow();
			super.horizontalScrollPosition = value;
		}
		
		public override function set htmlText( value:String ):void
		{
			super.htmlText = value;
			_textHasChanged = true;
		}
		
		public function get maxVerticalScrollPosition():int
		{
			return field.maxScrollV;
		}
		
		public override function set text( value:String ):void
		{
			if ( value == "" )
			{
				return;
			}
			super.text = value;	
			_textHasChanged = true;
		}
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		public function set verticalScrollPolicy( value:String ):void
		{
			_verticalScrollPolicy = value;
			invalidate( InvalidationType.SIZE );
		}
		
		public function get verticalScrollPosition():int
		{
			return field.scrollV;
		}
		public function set verticalScrollPosition( value:int ):void
		{
			drawNow();
			field.scrollV = value;
		}
		
		public override function get textHeight():Number
		{
			drawNow();
			return super.textHeight;
		}
		
		public override function get textWidth():Number
		{
			drawNow();
			return super.textWidth;
		}
		
		public function get wordWrap():Boolean
		{
			return _wordWrap;
		}
		public function set wordWrap( value:Boolean ):void
		{
			_wordWrap = value;
			invalidate( InvalidationType.STATE );
		}
		
		
		protected function needVScroll():Boolean
		{
			if( verticalScrollPolicy == ScrollPolicy.OFF ) { return false; }
			if( verticalScrollPolicy == ScrollPolicy.ON ) { return true; }
			return ( field.maxScrollV > 1 );
		}
		
		protected override function updateTextFieldType():void
		{
			super.updateTextFieldType();
			field.wordWrap = wordWrap;
			field.multiline = true;
		}
		
		
		protected function handleScroll( evt:ScrollEvent ):void
		{
			dispatchEvent( evt );
		}
		
	}
}